package com.easidea.tim.service.finance;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springside.core.service.EntityService;

import com.easidea.tim.dao.finance.ShouldPayCleanRecordDao;
import com.easidea.tim.dao.finance.ShouldPayDao;
import com.easidea.tim.dao.finance.ShouldPaySummaryDao;
import com.easidea.tim.dao.purchase.PurchaseOrderDao;
import com.easidea.tim.entity.finance.ShouldPay;
import com.easidea.tim.entity.finance.ShouldPayCleanRecord;
import com.easidea.tim.entity.finance.ShouldPaySummary;
import com.easidea.tim.util.DateUtil;
import com.easidea.tim.util.Page;

public class ShouldPayCleanRecordService extends
		EntityService<ShouldPayCleanRecord, ShouldPayCleanRecordDao> {

	private ShouldPayCleanRecordDao shouldPayCleanRecordDao;
	
	private ShouldPayDao shouldPayDao;
	
	private ShouldPaySummaryDao shouldPaySummaryDao;
	
	public void setShouldPaySummaryDao(ShouldPaySummaryDao shouldPaySummaryDao) {
		this.shouldPaySummaryDao = shouldPaySummaryDao;
	}
	public void setShouldPayDao(ShouldPayDao shouldPayDao) {
		this.shouldPayDao = shouldPayDao;
	}
	private PurchaseOrderDao purchaseOrderDao;
	/**
	 * 根据条件查询采购支付列表
	 * @param startDate
	 * @param endDate
	 * @param flowNo
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Page getShouldPayCleanRecordListByCondition(
			Date startDate, Date endDate, long orderId,
			long supId, Page page) {
		Criteria ca = this.getShouldPayCleanRecordListByConditionCriteria(startDate, endDate, orderId,supId);
		ca.setProjection(Projections.count("id"));
		Integer totalNum = (Integer) ca.uniqueResult();
		Criteria ca1 = this.getShouldPayCleanRecordListByConditionCriteria(startDate, endDate, orderId, supId);
		int startIndex = Page.getStartOfPage(page.getCurrentPageNo(), page.getPageSize());
		ca1.setFirstResult(startIndex);
		ca1.setMaxResults(page.getPageSize());
		List<ShouldPayCleanRecord> ls = (List<ShouldPayCleanRecord>) ca1.list();
		Page resultPage = new Page(startIndex, totalNum, page.getPageSize(), ls);
		resultPage.setCurrentPageNo(page.getCurrentPageNo());
		
		return resultPage;
	}
	private Criteria getShouldPayCleanRecordListByConditionCriteria(
			Date startDate, Date endDate, long orderId, long supId) {
		Criteria ca = createUnDeletedCriteria();
		ca.createAlias("order", "order");
		ca.createAlias("order.supplier", "supplier");		
		if(startDate != null && !startDate.equals("") && endDate != null && !endDate.equals("") ) {
			ca.add(Restrictions.between("payDate", DateUtil.getStartTime(startDate), DateUtil.getEndTime(endDate)));
		}
		if (orderId != -1) {
			ca.add(Restrictions.eq("order.id", orderId));
		}
		if (supId != -1) {
			ca.add(Restrictions.eq("supplier.id", supId));
		}	
		return ca;
	}
	 /**
	  *获取采购合同流水号和Id 
	  * @return
	  */
	public List<Object[]> getAllPurchaseOrderList(){
		return this.purchaseOrderDao.getAllFlowNos();
	}
	public String savePay(ShouldPayCleanRecord pay) {
		try{
		
		 ShouldPay shouldPay = shouldPayDao.getShouldPayByOrderId(pay.getOrder().getId());
		 ShouldPaySummary paySummary = shouldPay.getShouldPaySummary();
		 pay.setPayDate(new Date());
		 pay.setShouldPay(shouldPay);
		 this.shouldPayCleanRecordDao.getHibernateTemplate().save(pay);
		 paySummary.setPayed(paySummary.getPayed() + pay.getAmount());
		 if (paySummary.getAmount() == paySummary.getPayed()) {
			 paySummary.setCleared(ShouldPaySummary.PAY_CLEARED_YES);
		 }
		this.shouldPaySummaryDao.getHibernateTemplate().update(paySummary);
		}catch(DataAccessException de) {
			de.printStackTrace();
			return de.getMessage();
		}
		return "success";		
	}
	public Page getShouldPayListByCondition(
			Date startDate, Date endDate, 
			String supName,String responser,Page page) {
		return this.shouldPaySummaryDao.getShouldPayListByCondition(startDate, endDate, supName, responser, page);
	}
	@Override
	public ShouldPayCleanRecordDao getEntityDao() {
		// TODO Auto-generated method stub
		return shouldPayCleanRecordDao;
	}

	public void setShouldPayCleanRecordDao(
			ShouldPayCleanRecordDao shouldPayCleanRecordDao) {
		this.shouldPayCleanRecordDao = shouldPayCleanRecordDao;
	}
	public PurchaseOrderDao getPurchaseOrderDao() {
		return purchaseOrderDao;
	}
	public void setPurchaseOrderDao(PurchaseOrderDao purchaseOrderDao) {
		this.purchaseOrderDao = purchaseOrderDao;
	}

}
